{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# KNN手写数字识别分类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn import preprocessing\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.datasets import load_digits\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1797, 64)\n"
     ]
    }
   ],
   "source": [
    "#加载数据\n",
    "digits = load_digits()\n",
    "data = digits.data\n",
    "#数据探索\n",
    "print(data.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  0.   0.   5.  13.   9.   1.   0.   0.]\n",
      " [  0.   0.  13.  15.  10.  15.   5.   0.]\n",
      " [  0.   3.  15.   2.   0.  11.   8.   0.]\n",
      " [  0.   4.  12.   0.   0.   8.   8.   0.]\n",
      " [  0.   5.   8.   0.   0.   9.   8.   0.]\n",
      " [  0.   4.  11.   0.   1.  12.   7.   0.]\n",
      " [  0.   2.  14.   5.  10.  12.   0.   0.]\n",
      " [  0.   0.   6.  13.  10.   0.   0.   0.]]\n"
     ]
    }
   ],
   "source": [
    "# 查看第一幅图像\n",
    "print(digits.images[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "# 第一幅图像代表的数字含义\n",
    "print(digits.target[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "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5n0DM1yfaI7T22nx/0LE2yfbHtnbYP2J6xffWgZ+rHwE/Rq2ut/1MLV4yZk/SmpM0RsX+gg/XJ\n9qWSLo2I3bYvlDQt6SfDfr9Osf0LSWskfSMibhr0PE2x/bikP0XE9upCo+dHxIeDnutsdeEIvlbS\nwYiYjYjjkp6SdMuAZ+pbRLwbEbur9z+SNCNpdLBTNcP2mKQbJW0f9CxNsn2RpGskPSJJEXF8mOOW\nuhH4qKRDp308pyQhnGJ7maRVkt4Y7CSNeUjSvZI+HfQgDVsu6Yikx6ofP7ZX1yMcWl0IPDXbF0h6\nWtKWiDg26Hn6ZfsmSYcjYnrQs7TgHElXSXo4IlZJ+kTSUD8m1IXA5yUtPe3jsepzQ8/2uVqIe0dE\nZLki7TpJN9t+Rws/Tq23/cRgR2rMnKS5iDh1prVTC8EPrS4E/qaky20vrx7U2CTpuQHP1Dfb1sLP\ncjMR8eCg52lKRNwfEWMRsUwLf1cvR8RtAx6rERHxnqRDtldWn7pW0lA/KLrYvckaFxEnbN8l6SVJ\nI5IejYh9Ax6rCesk3S7pH7b3Vp/7VUS8MMCZ0NvdknZUB5tZSXcMeJ6+DPzXZADa04VTdAAtIXAg\nMQIHEiNwIDECBxIjcCAxAgcSI3Agsf8BewWNdaq60rcAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x281f0968630>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 将第一幅图像显示出来\n",
    "plt.gray()\n",
    "plt.imshow(digits.images[0])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 分割数据，将25%的数据作为测试集，其余作为训练集\n",
    "train_x, test_x, train_y, test_y = train_test_split(data, digits.target, test_size=0.25, random_state=33)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 采用Z-Score规范化\n",
    "ss = preprocessing.StandardScaler()\n",
    "train_ss_x = ss.fit_transform(train_x)\n",
    "test_ss_x = ss.transform(test_x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "KNN准确率: 0.9733\n"
     ]
    }
   ],
   "source": [
    "# 创建KNN分类器\n",
    "knn = KNeighborsClassifier(n_neighbors=4)\n",
    "knn.fit(train_ss_x, train_y) \n",
    "predict_y = knn.predict(test_ss_x) \n",
    "print(\"KNN准确率: %.4lf\" % accuracy_score(test_y, predict_y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
